package pl.peterjack.servlet.dane.firma;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import pl.peterjack.model.PMF;
import pl.peterjack.model.Stale;
import pl.peterjack.model.dane.Firma;
import pl.peterjack.model.dane.Klient;
import pl.peterjack.model.dane.Pracownik;
import pl.peterjack.model.dane.RezultatOdpowiedz;

import com.google.gson.Gson;



public class FirmaLogowanie extends HttpServlet {
	
	

	Log log = new LogFactoryImpl().getInstance(FirmaLogowanie.class);

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp); 
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String login = "" + req.getParameter("login"); 
		String haslo = req.getParameter("haslo")+"";
		// TODO szyfrowanie hasla
		/*try {
    		MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
    		digest.update(haslo.getBytes());
    		haslo = digest.digest().toString();
		} catch (Exception e) {
		}*/
		@SuppressWarnings("rawtypes")
		List results = null; 
		Firma firma = null; 
		Klient klient = null; 
		Pracownik pracownik = null; 
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(Stale.LOGOWANIE_FIRMA.equals(req.getParameter("logowanieTyp"))) {
			try {
				String queryStr = "select from " + Firma.class.getName() + 
		        " where login == :p1 && haslo == :p2  ";
			    results = (List) pm.newQuery(queryStr).execute(login, haslo);
			    if(results != null && results.size() > 0) {
			    	firma = (Firma) results.get(0); 
			    }
			} catch (Exception e) { 
				log.error(e);
			}
		} else if(Stale.LOGOWANIE_PRACOWNIK.equals(req.getParameter("logowanieTyp"))) {
			try {
				String queryStr = "select from " + Pracownik.class.getName() + 
		        " where login == :p1 && haslo == :p2  ";
			    results = (List) pm.newQuery(queryStr).execute(login, haslo);
			    if(results != null && results.size() > 0) {
			    	pracownik = (Pracownik) results.get(0);
			    	firma = pm.getObjectById(Firma.class, pracownik.getFirma());
			    }
			} catch (Exception e) { 
				log.error(e);
			}
		} else if(Stale.LOGOWANIE_KLIENT.equals(req.getParameter("logowanieTyp"))) {
			try {
				String queryStr = "select from " + Klient.class.getName() + 
		        " where login == :p1 && haslo == :p2  ";
			    results = (List) pm.newQuery(queryStr).execute(login, haslo);
			    if(results != null && results.size() > 0) {
			    	klient = (Klient) results.get(0);
			    	firma = pm.getObjectById(Firma.class, klient.getFirma());
			    }
			} catch (Exception e) { 
				log.error(e);
			}
		}
		// zapisujemy
	    RezultatOdpowiedz result = new RezultatOdpowiedz();
    	result.setSuccess( results != null && results.size() > 0 && results.get(0)!=null );
    	if( !(results != null && results.size() > 0 ) ) {
    		// sprawdzamy klientow
    		result.setMessage(new String("Bledny login lub haslo. ".getBytes(), "UTF-8"));	
    	} 
    	
    	
    	if( results != null && results.size() > 0  ) {
    		// ustawiamy sesje 
    		req.getSession().setAttribute("login", login); 
    		req.getSession().setAttribute("haslo", haslo); 
    		req.getSession().setAttribute("firma_id", firma.getId());
    		req.getSession().setAttribute("firma", firma); 
    		req.getSession().setAttribute("firma_nazwa", firma.getNazwa());
    		req.getSession().setAttribute("firma_klient", klient);
    		req.getSession().setAttribute("firma_pracownik", pracownik);
    	}
	    Gson gson = new Gson();
	    //resp.setContentType("application/json");
	    resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println(gson.toJson(result));
        pm.close();
        /*
         * Tymaczosowo bez szyfrowania - admin test
         */
	}

	

}
